package com.allwees.bs.module.oms.service.impl;

import com.neovisionaries.i18n.CurrencyCode;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.oms.constant.EFmsAccountLogBsType;
import com.allwees.bs.module.oms.constant.EFmsBalanceType;
import com.allwees.bs.module.oms.dao.FmsRetailerSettleStatementDao;
import com.allwees.bs.module.oms.entity.FmsMerchantAccount;
import com.allwees.bs.module.oms.entity.FmsMerchantAccountLog;
import com.allwees.bs.module.oms.entity.FmsRetailerSettleStatement;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.oms.service.FmsMerchantAccountLogService;
import com.allwees.bs.module.oms.service.FmsRetailerSettleStatementService;
import com.allwees.bs.module.oms.util.FmsNoUtil;
import com.allwees.core.common.util.MoneyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 *
 *
 * @author yangylsky
 * @since 2019-12-12
 *
 */
@Service
public class FmsRetailerSettleStatementServiceImpl extends BoServiceImpl<FmsRetailerSettleStatementDao, FmsRetailerSettleStatement>
		implements FmsRetailerSettleStatementService {
	@Autowired
	private FmsMerchantAccountLogService merchantAccountLogService;




	public FmsRetailerSettleStatement createSettleStatement(String retailerUuid, OmsOrderItem omsOrderItem) {
		FmsRetailerSettleStatement settleStatement = new FmsRetailerSettleStatement();
		settleStatement.setRetailerUuid(retailerUuid);
		settleStatement.setOrderNo(omsOrderItem.getOrderNo());
		settleStatement.setOrderItemUuid(omsOrderItem.getUuid());
		settleStatement.setOrderUuid(omsOrderItem.getOrderUuid());
		settleStatement.setNo(FmsNoUtil.genSettleStatementNo(EBranchType.RETAILER, retailerUuid));
		settleStatement.setCurrency(CurrencyCode.USD.name());
		BigDecimal amountTotal = BigDecimal.ZERO;

		BigDecimal skuAmount = MoneyUtil.mul(omsOrderItem.getPrice(), BigDecimal.valueOf(omsOrderItem.getQuantity()));
		BigDecimal skuShippingAmount = MoneyUtil.mul(omsOrderItem.getShippingPrice(), BigDecimal.valueOf(omsOrderItem.getQuantity()));
		amountTotal = MoneyUtil.add(amountTotal, skuAmount,skuShippingAmount);

		settleStatement.setAmount(amountTotal);
		save(settleStatement);
		return settleStatement;
	}

	public void logSettle(FmsMerchantAccount account, FmsRetailerSettleStatement settleStatement) {
		FmsMerchantAccountLog accountLog = new FmsMerchantAccountLog();
		accountLog.setBranchUuid(account.getBranchUuid())
				.setBalanceType(EFmsBalanceType.INCOME)
				.setBsType(EFmsAccountLogBsType.ORDER_SETTLED)
				.setNo(FmsNoUtil.genAccountLogNo(account.getBranchUuid(), EFmsBalanceType.INCOME, EFmsAccountLogBsType.ORDER_SETTLED))
				.setAccountUuid(account.getUuid())
				.setAccountNo(account.getAccountNo())
				.setTakeAmount(settleStatement.getAmount())
				.setCurrency(settleStatement.getCurrency())
				.setFee(BigDecimal.ZERO)
				.setBalance(account.getBalance())
				.setBalanceCurrency(account.getCurrency());
		merchantAccountLogService.create(accountLog);
	}
}
